//
//  XClockInListViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XClockInListViewController.h"
#import <LYEmptyView/LYEmptyView.h>
#import <LYEmptyView/UIView+Empty.h>
#import "XClockInTableViewCell.h"

#import "XClockInDetailsViewController.h"
#import "XClockInEditViewController.h"

@interface XClockInListViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 列表*/
@property(nonatomic,strong)UITableView* tableView;


@property(nonatomic,strong)RLMResults<XClockInModel*> *datas;

@end

@implementation XClockInListViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNavBar];
    
    [self setupTableView];
}


- (void)setupNavBar{
    
    self.navigationItem.title = @"打卡";

//    UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [closeBtn setImage:[UIImage imageNamed:@"a_add_navbar"] forState:(UIControlStateNormal)];
//    [closeBtn addTarget:self action:@selector(addClicked:) forControlEvents:UIControlEventTouchUpInside];
//    closeBtn.titleLabel.font = FONT(14);
//    closeBtn.myHeight = PX(44);
//    closeBtn.myWidth = PX(44);
//
//    UIBarButtonItem* modidyItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
//    self.navigationItem.rightBarButtonItem = modidyItem;
}

-(void)setupTableView{
    
    //列表
    UITableView* tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), SCREEN_HEIGHT - IPHONE_NAVIGATION_BAR_HEIGHT-IPHONE_TAB_BAR_HEIGHT) style:UITableViewStyleGrouped];

    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = COLOR_BACKGEOUND;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.estimatedRowHeight = PX(100);
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.sectionFooterHeight = CGFLOAT_MIN;
    tableView.sectionHeaderHeight = 10;
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
    
    [tableView registerClass:NSClassFromString(clockInCell) forCellReuseIdentifier:clockInCell];
    [self.view addSubview:tableView];
    _tableView = tableView;
    

    LYEmptyView* emptyView = [LYEmptyView emptyActionViewWithImage:NULL titleStr:@"发现不一样的自己" detailStr:NULL btnTitleStr:@"设置打卡" target:self action:@selector(addClicked:)];
    emptyView.titleLabFont = FONT_FIT(13);
    emptyView.titleLabTextColor = COLOR_GRAY_BASE;
    emptyView.detailLabMargin = 10;
    emptyView.actionBtnFont = FONT_FIT(12);
    emptyView.actionBtnWidth = PX(120);
    emptyView.actionBtnHeight = PX(32);
    emptyView.actionBtnCornerRadius = PX(16);
    emptyView.actionBtnBackGroundColor = COLOR_THEME_A;
    emptyView.actionBtnTitleColor = COLOR_WHITE;
    emptyView.actionBtnMargin = 20;
    emptyView.contentViewOffset = -100;
    tableView.ly_emptyView = emptyView;
    
    
    UIButton* addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"a_item_add"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addClicked:) forControlEvents:UIControlEventTouchUpInside];
//    addBtn.titleLabel.font = FONTBOLD_FIT(20);
    addBtn.frame = CGRectMake(self.view.rs_width - 88, CGRectGetMaxY(tableView.frame) - 88, 88, 88);
//    addBtn.layer.cornerRadius = PX(40)/2.0f;
//    addBtn.layer.masksToBounds = YES;
//    addBtn.layer.borderColor = COLOR_THEME_A.CGColor;
//    addBtn.layer.borderWidth = 2.0f;
    
    [self.view addSubview:addBtn];
}

#pragma mark  -- add
- (void)addClicked:(UIButton*)sender{

    XClockInEditViewController* editController = [XClockInEditViewController new];
    editController.operation = EditOperation_Add;
    editController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        [self reloadData];
    };
    [UIViewController showViewController:editController animated:YES];
}

#pragma mark -- reloadData
// 刷新数据
- (void)reloadData{
    
    RLMResults<XClockInModel*> *models = [XClockInModel allObjects];
    self.datas = models;
    [self.tableView reloadData];
    
    [self.tableView reloadData];
}

#pragma mark --UITableViewDelegate  UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XClockInTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:clockInCell forIndexPath:indexPath];
    cell.model = [self.datas objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    XClockInDetailsViewController* detailController = [XClockInDetailsViewController new];
    detailController.model = [self.datas objectAtIndex:indexPath.row];
    [UIViewController showViewController:detailController animated:YES];
}

@end
